package appcus

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/app"
	"errors"
	"gorm.io/gorm"
)

type CustomizationService struct{}

// CreateCustomization 创建新的定制请求
func (service *CustomizationService) CreateCustomization(req app.Customization) error {
	return global.GVA_DB.Create(&req).Error
}

// 根据是否处理状态获取所有定制请求
func (service *CustomizationService) GetAllCustomizationsByStatus(isHandled bool) ([]app.Customization, error) {
	var customizations []app.Customization
	err := global.GVA_DB.Where("is_handled = ?", isHandled).Find(&customizations).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, nil
	}
	return customizations, err
}

// MarkAsHandled 标记请求为已处理
func (service *CustomizationService) MarkAsHandled(id uint, isHandled bool) error {
	var req app.Customization
	if err := global.GVA_DB.First(&req, id).Error; err != nil {
		return errors.New("定制请求未找到")
	}
	req.IsHandled = isHandled
	return global.GVA_DB.Save(&req).Error
}
